// create.js
var activityService = require('../../services/activityService.js');
Date.prototype.Format = function (fmt) { //author: meizz 
  var o = {
    "M+": this.getMonth() + 1, //月份 
    "d+": this.getDate(), //日 
    "h+": this.getHours(), //小时 
    "m+": this.getMinutes(), //分 
    "s+": this.getSeconds(), //秒 
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
    "S": this.getMilliseconds() //毫秒 
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active_time_start:{},
    active_time_end: {},
    enter_time_start: {},
    enter_time_end: {},
    activity_type:[
      {
        id:1,
        title:'社区任务'
      },
      {
        id:2,
        title: '老人任务'
      },
      {
        id: 3,
        title: '团队任务'
      },
      {
        id: 4,
        title: '党建活动'
      }
    ],
    join_method: [
      {
        id: 1,
        title: '开放参与'
      },
      {
        id: 2,
        title: '限定团队组织成员参与'
      },
    ],
    activity_index:0,
    join_method_index:0,
    cityName:[],
    cityId:[],
    formData:{},
    picture:'../../images/addpic.png',
    file_path:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initTime();
  },

  onShow:function(){
    console.log(app.globalData.selectedCity)
    this.initCity();
  },
  initTime:function(){
    var myDate = new Date();
    console.log(myDate.Format("hh:mm"));
    this.setData({
      active_time_start: { date: myDate.Format("yyyy-MM-dd"), time: myDate.Format("hh:mm")},
      active_time_end: { date: myDate.Format("yyyy-MM-dd"), time: myDate.Format("hh:mm") },
      enter_time_start: { date: myDate.Format("yyyy-MM-dd"), time: myDate.Format("hh:mm") },
      enter_time_end: { date: myDate.Format("yyyy-MM-dd"), time: myDate.Format("hh:mm") },
    })
  },
  initCity:function(){
    var that = this
    activityService.getCityName({
      id: app.globalData.selectedCity,
      success: function (res) {
        if (res) {
          console.log(app.globalData.selectedCity);
          console.log(that.data.cityId);
          var in_array = false;
          for (var i = 0; i < that.data.cityId.length;i++){
            if (app.globalData.selectedCity == that.data.cityId[i]){
              in_array = true;
              break;
            }
          }
          if (!in_array){
            that.data.cityId.push(app.globalData.selectedCity);
            that.data.cityName.push(res.name);
          }
          that.setData({
            cityName: that.data.cityName,
          })
        }
      }
    })
  },
  formSubmit: function (e) {
    this.data.formData.subject = e.detail.value.subject;
    this.data.formData.min_volunteer_num = e.detail.value.min_volunteer_num;
    this.data.formData.time_length = e.detail.value.time_length;
    this.data.formData.volunteer_num = e.detail.value.volunteer_num;
    this.data.formData.description = e.detail.value.description;
    this.data.formData.volunteer_rule = e.detail.value.volunteer_rule;
    this.data.formData.enter_time_start = this.data.enter_time_start.date + ' ' + this.data.enter_time_start.time + ':00';
    this.data.formData.enter_time_end = this.data.enter_time_end.date + ' ' + this.data.enter_time_end.time + ':00';
    this.data.formData.active_time_start = this.data.active_time_start.date + ' ' + this.data.active_time_start.time+':00';
    this.data.formData.active_time_end = this.data.active_time_end.date + ' ' + this.data.active_time_end.time + ':00';
    this.data.formData.activity_type = this.data.activity_type[this.data.activity_index].id;
    this.data.formData.join_method = this.data.join_method[this.data.join_method_index].id;
    this.data.formData.user_id = app.globalData.apiUser;
    this.data.formData.city_id = this.data.cityId;
    this.data.formData.picture = this.data.file_path;
    console.log('form发生了submit事件，携带数据为：', this.data.formData)
    activityService.createActivity({
      form_data: this.data.formData,
      success:function(response){

      }
    });
  },
  active_time_start_date_set:function(e){
    this.data.active_time_start.date = e.detail.value;
    this.setData({
      active_time_start: this.data.active_time_start
    })
  },
  active_time_start_time_set: function (e) {
    this.data.active_time_start.time = e.detail.value;
    this.setData({
      active_time_start: this.data.active_time_start
    })
  },
  active_time_end_date_set: function (e) {
    this.data.active_time_end.date = e.detail.value;
    this.setData({
      active_time_end: this.data.active_time_end
    })
  },
  active_time_end_time_set: function (e) {
    this.data.active_time_end.time = e.detail.value;
    this.setData({
      active_time_end: this.data.active_time_end
    })
  },
  enter_time_start_date_set: function (e) {
    this.data.enter_time_start.date = e.detail.value;
    this.setData({
      enter_time_start: this.data.enter_time_start
    })
  },
  enter_time_start_time_set: function (e) {
    this.data.enter_time_start.time = e.detail.value;
    this.setData({
      enter_time_start: this.data.enter_time_start
    })
  },
  enter_time_end_date_set: function (e) {
    this.data.enter_time_end.date = e.detail.value;
    this.setData({
      enter_time_end: this.data.enter_time_end
    })
  },
  enter_time_end_time_set: function (e) {
    this.data.enter_time_end.time = e.detail.value;
    this.setData({
      enter_time_end: this.data.enter_time_end
    })
  },
  set_activity_type:function(e){
    this.data.activity_index = e.detail.value;
    this.setData({
      activity_index: this.data.activity_index
    })
  },
  set_join_type: function (e) {
    this.data.join_method_index = e.detail.value;
    this.setData({
      join_method_index: this.data.join_method_index
    })
  },
  selectCity:function(){
    wx.navigateTo({
      url: 'selectcity',
    })
  },
  uploadImg:function(){
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var file = res.tempFiles;
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        that.setData({
          picture: tempFilePaths,
        })

        wx.uploadFile({
          url: app.globalData.apiHost + '/backstage/api/upload', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (res) {
            var info = JSON.parse(res.data);
            that.setData({
              file_path: info.fileId
            })
          }
        })
      }
    })
  }
})